package com.cpi.cornpp.plantversion.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.plantversion.exception.PlantVersionException;
import com.cpi.cornpp.plantversion.form.PlantVersionForm;
import com.cpi.cornpp.plantversion.services.PlantVersionService;
import com.cpi.cornpp.plantversion.view.PlantVersionGroupView;
import com.cs.common.utils.NumberUtil;

public class PlantVersionAction extends MappingDispatchAction{
	/** Default forward */
	private final static String DEFAULT_FORWARD = "default";//$NON-NLS-1$
	/** Custom exception */
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	/** Id de version */
	private final static String VERSION_ID_LABEL = "versionId";//$NON-NLS-1$
	/** Information message name*/
	private final static String MESSAGE = "MESSAGE";//$NON-NLS-1$
	/** Success edit key */
	private static final String SUCCESS_EDIT_LABEL = "common.edit.success";//$NON-NLS-1$
	private static final String PLAN_VERSION_GROUP_VIEW = "PlantVersionGroupsView";//$NON-NLS-1$
	private static final String SUM_GRIND_LABEL = "SUM_GRIND_LABEL";//$NON-NLS-1$
	public ActionForward showPlantsVersion(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		HttpSession httpSession = null;
		PlantVersionService plantVersionService = null;
		int versionId = 0;
		List plantVersionGroupsView = null;
		PlantVersionForm plantVersionForm = null;
		try{
			httpSession = request.getSession();
			plantVersionForm = (PlantVersionForm)form;
			versionId = NumberUtil.getInt(httpSession.getAttribute(VERSION_ID_LABEL));
			plantVersionService = new PlantVersionService();
			plantVersionGroupsView = plantVersionService.getPlantsVersions(versionId);
			httpSession.setAttribute(PLAN_VERSION_GROUP_VIEW, plantVersionGroupsView);
			if( plantVersionForm == null ){
				plantVersionForm = new PlantVersionForm();
			}
			fillForm(plantVersionForm,plantVersionGroupsView);
			request.setAttribute(SUM_GRIND_LABEL, plantVersionForm.getSumGrind());
		}catch (PlantVersionException plantVersionException){
			httpSession.setAttribute(CUSTOM_EXCEPTION, plantVersionException);
			throw plantVersionException;
		}catch (Exception exception){
			PlantVersionException plantVersionException = null;
			exception.printStackTrace(System.out);
			plantVersionException= new PlantVersionException(exception, CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			httpSession.setAttribute(CUSTOM_EXCEPTION, plantVersionException);
			throw plantVersionException;
		}
		return mapping.findForward(DEFAULT_FORWARD);
	}
	
	public ActionForward savePlantsVersion(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		HttpSession httpSession = null;
		PlantVersionService plantVersionService = null;
		PlantVersionForm plantVersionForm = null;
		MessageResources messageResources = null;
		String messageError = null;
		try{
			httpSession = request.getSession();
			plantVersionForm = (PlantVersionForm)form;
			plantVersionService = new PlantVersionService();
			messageResources = getResources(request);
			plantVersionService.updatePlantsVersions(plantVersionForm.getPlantVersionGroupsView());
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), SUCCESS_EDIT_LABEL);
			httpSession.setAttribute(MESSAGE, messageError);
		}catch (PlantVersionException plantVersionException){
			httpSession.setAttribute(CUSTOM_EXCEPTION, plantVersionException);
			throw plantVersionException;
		}catch (Exception exception){
			PlantVersionException plantVersionException = null;
			exception.printStackTrace(System.out);
			plantVersionException= new PlantVersionException(exception, CornPPException.LAYER_ACTION,CornPPException.ACTION_UPDATE);
			httpSession.setAttribute(CUSTOM_EXCEPTION, plantVersionException);
			throw plantVersionException;
		}
		return mapping.findForward(DEFAULT_FORWARD);
	}
	
	private void fillForm(PlantVersionForm plantVersionForm, List plantVersionGroupsView) {
		Iterator iterator = null;
		ArrayList plantsUnified = null;
		PlantVersionGroupView plantVersionGroupView = null;
		HashMap sumGrind = null;
		Object dummy = null;
		plantsUnified = new ArrayList(0);
		iterator = plantVersionGroupsView.iterator();
		int indexSumGrind = -1;
		while( iterator.hasNext() ){
			indexSumGrind++;
			dummy = iterator.next();
			if( dummy instanceof HashMap ){
				sumGrind = (HashMap)dummy;
			}else{
				plantVersionGroupView = (PlantVersionGroupView)dummy;
				plantsUnified.addAll(plantVersionGroupView.getMonthPeriodsVersion() );
			}
		}
		plantVersionForm.setPlantVersionGroupsView( plantsUnified );
		plantVersionForm.setSumGrind(sumGrind);
		plantVersionGroupsView.remove(indexSumGrind);
	}	
}